// Created by MyEclipse Struts
// XSL source (default):
// platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_3.8.4/xslt/JavaClass.xsl

package com.novix.struts.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanComparator;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.novix.portal.framework.model.IPortalUser;
import com.novix.portal.framework.model.impl.WebContact;
import com.novix.portal.framework.service.IGenericService;
import com.novix.portal.framework.util.IConstants;
import com.novix.portal.framework.util.UserContainer;

/**
 * MyEclipse Struts Creation date: 05-30-2005 XDoclet definition:
 * 
 * @struts:action validate="true"
 */
public class ViewContactsAction extends PortalBaseAction {

    // --------------------------------------------------------- Instance
    // Variables

    // --------------------------------------------------------- Methods

    private static final String COMPANYNAME   = "companyName";
    private static final String COMPANYID     = "companyId";
    private static final String SHOWALL       = "showall";
    private static final String CATEGORY      = "category";
    private static final String COMPANYFILTER = "companyFilter";
    private static final String LASTNAME      = "lastname";
    private static final String BOARD_INVITEE = "Board Invitee";
    private static final String COMPNAME      = "compname";

    /**
     * Method execute
     * 
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {

        UserContainer uc = (UserContainer) getUserContainer(request);
        IPortalUser user = uc.getUser();

        String id = request.getParameter(COMPANYID);
        if (StringUtils.isEmpty(id))
            request.getSession().setAttribute(COMPANYFILTER, null);
        else
            request.getSession().setAttribute(COMPANYFILTER, id);

        String category = request.getParameter(CATEGORY);
        if (StringUtils.isEmpty(category))
            request.getSession().setAttribute(CATEGORY, null);
        else
            request.getSession().setAttribute(CATEGORY, category);

        String showAll = request.getParameter(SHOWALL);
        if (showAll == null)
            showAll = "false";
        boolean showEverybody = new Boolean(showAll).booleanValue();
        IGenericService service = getPortalService();
        List selectedCompanyContactsList = new ArrayList();
        List completeList = new ArrayList();
        List filteredList = new ArrayList();
        completeList = service.getAllContactsByCaptivePicsOrNone(uc.getUser());
        request.getSession().setAttribute(IConstants.WEB_CONTACT_COMPLETE_LIST, completeList);
        if (id != null) {
            for (Iterator iter = completeList.iterator(); iter.hasNext();) {
                WebContact element = (WebContact) iter.next();
                if (category == null) {
                    if (element.getCompanyId().compareToIgnoreCase(id) == 0)
                        filteredList.add(element);
                } else if (category != null) {
                    if (element.getCompanyId().compareToIgnoreCase(id) == 0 && element.getCategories().contains(BOARD_INVITEE))
                        filteredList.add(element);
                }

            }
            request.getSession().setAttribute(IConstants.WEB_CONTACT_COMPLETE_LIST, filteredList);
        }
        List list = (List) request.getSession().getAttribute(IConstants.WEB_CONTACT_COMPLETE_LIST);
        filterByLastName(request, showEverybody, selectedCompanyContactsList, list);
        if (request.getParameter(COMPNAME) != null) 
            selectedCompanyContactsList = filterByCompName(request, showEverybody, list);
        BeanComparator comp = new BeanComparator(COMPANYNAME);
        Collections.sort(selectedCompanyContactsList, comp);
        request.setAttribute(IConstants.SELECTED_COMPANY_CONTACTS_LIST, selectedCompanyContactsList);
        return mapping.findForward(IConstants.SUCCESS);
    }

    /**
     * @param request
     * @param showEverybody
     * @param selectedCompanyContactsList
     * @param list
     */
    private List filterByCompName(HttpServletRequest request, boolean showEverybody, List list) {
        if (request.getParameter(COMPNAME) == null) {
            return null;
        } else {
            List selectedCompanyContactsList  = new ArrayList();
            selectedCompanyContactsList = new ArrayList();
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                WebContact element = (WebContact) iter.next();
                if (!showEverybody) {
                    if (element.getCompanyName().startsWith(request.getParameter(COMPNAME)) && element.getImageLinkHtml().compareToIgnoreCase(IConstants.NO_IMAGE_FOUND) != 0) {
                        selectedCompanyContactsList.add(element);
                    }
                } else {
                    if (element.getLastName().startsWith(request.getParameter(COMPNAME))) {
                        selectedCompanyContactsList.add(element);
                    }
                }
            }
            return selectedCompanyContactsList;
        }
    }

    /**
     * @param request
     * @param showEverybody
     * @param selectedCompanyContactsList
     * @param list
     */
    private void filterByLastName(HttpServletRequest request, boolean showEverybody, List selectedCompanyContactsList, List list) {
        if (request.getParameter(LASTNAME) == null) {
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                WebContact element = (WebContact) iter.next();

                if (!showEverybody) {
                    if (element.getImageLinkHtml().compareToIgnoreCase(IConstants.NO_IMAGE_FOUND) != 0)
                        selectedCompanyContactsList.add(element);
                } else
                    selectedCompanyContactsList.add(element);
            }

        } else {
            for (Iterator iter = list.iterator(); iter.hasNext();) {
                WebContact element = (WebContact) iter.next();
                if (!showEverybody) {
                    if (element.getLastName().startsWith(request.getParameter(LASTNAME)) && element.getImageLinkHtml().compareToIgnoreCase(IConstants.NO_IMAGE_FOUND) != 0) {
                        selectedCompanyContactsList.add(element);
                    }
                } else {
                    if (element.getLastName().startsWith(request.getParameter(LASTNAME))) {
                        selectedCompanyContactsList.add(element);
                    }
                }
            }
        }
    }
}
